﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Compression;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;

namespace DPKFileDownload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtPath.Text = folderDialog.SelectedPath;
            }
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPath.Text))
            {
                MessageBox.Show("Please enter TFS Folder!");
                txtPath.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtDPK.Text))
            {
                MessageBox.Show("Please enter DPK Path!");
                txtDPK.Focus();
                return;
            }

            try
            {
                string directory = Environment.CurrentDirectory + "\\temp";
                if (Directory.Exists(directory))
                {
                    Directory.Delete(directory);
                }
                ZipFile.ExtractToDirectory(txtDPK.Text, directory);
                ArrayList list = ReadFile(directory + "\\" + "_MANIFEST");
                DirectoryInfo di = new DirectoryInfo(directory);
                FileInfo[] fileArray = di.GetFiles();
                int index = 0;
                foreach (FileInfo fi in fileArray)
                {
                    if (fi.Name.IndexOf("_changed") > 0)
                    {
                        Regex reg = new Regex("(?<=(_))[.\\s\\S]*?(?=(_v))", RegexOptions.Multiline | RegexOptions.Singleline);
                        string name = reg.Match(fi.Name).Value;
                        int i;
                        for (i = index; i < list.Count; i++)
                        {
                            if (list[i].ToString().IndexOf("/" + name) > 0)
                            {
                                string fileName = list[i].ToString().Substring(list[i].ToString().LastIndexOf(":") + 2);
                                fileName = fileName.Replace("/AuctionAdDelivery/FrontDoor/Dev", "");
                                fileName = txtPath.Text + fileName.Replace("/", "\\");

                                string strPath = Path.GetDirectoryName(fileName);
                                if (!Directory.Exists(strPath))
                                {
                                    Directory.CreateDirectory(strPath);
                                }
                                File.Copy(fi.FullName, fileName, true);
                                index = i + 1;                       
                                break;
                            }
                        }
                    }
                }
                Directory.Delete(directory, true);
                MessageBox.Show("Execute successed!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Execute failed!" + ex.Message);
            }
        }

        public ArrayList ReadFile(string fileName)
        {
            ArrayList list = new ArrayList();

            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    sr.BaseStream.Seek(0, SeekOrigin.Begin);

                    string str = sr.ReadLine();
                    while (str != null)
                    {
                        list.Add(str);
                        str = sr.ReadLine();
                    }

                    sr.Close();
                }
                fs.Close();
            }
            return list;
        }
    }
}
